package com.dropTables.TakingNote;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;


public class MapOverlay extends ItemizedOverlay<OverlayItem> 
{
	
	private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
	private Context mapContext;
	
	public String chosen_name;				//The name of the clicked overlay item
	public String chosen_loc;				//The location of the clicked overlay item
	LocationSelectMap parent;				//Parent map
	public MapOverlay(Drawable defaultMarker, Context context) 
	{
		super(boundCenterBottom(defaultMarker));
		mapContext = context;
		parent = (LocationSelectMap) context;
	}

	@Override
	protected OverlayItem createItem(int index) 
	{
		return mapOverlays.get(index);
	}
	
	public void addOverlay(OverlayItem overlay)
	{
		mapOverlays.add(overlay);
		populate();
	}

	@Override
	public int size()
	{
		return mapOverlays.size();
	}
	
	/*
	 * @param index Index of object clicked
	 * What to do when an object is clicked
	 */
	@Override
	protected boolean onTap(int index)
	{
		OverlayItem item = mapOverlays.get(index);
		chosen_name = item.getTitle();
		chosen_loc = item.getSnippet();
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(mapContext);			//Create a confirm popup
		dialog.setTitle("Set this as reminder location?");
		dialog.setMessage(item.getTitle());
		dialog.setCancelable(false);
		
		dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which) 
			{
				MapOverlay.this.parent.locSelected(MapOverlay.this.chosen_name, MapOverlay.this.chosen_loc);		//If yes call locSelected from map
			}
		});
		dialog.setNegativeButton("No", new DialogInterface.OnClickListener() 
		{
			
			@Override
			public void onClick(DialogInterface dialog, int which) 
			{
				dialog.cancel();
			}
		});
		AlertDialog alert = dialog.create();
		alert.show();
		return true;
	}

}
